package com.cola.action;

import com.cola.entity.export.ExportShop;
import com.cola.service.ShopService;
import com.cola.utils.Struts2Utils;
import com.cola.utils.excel.poi.ExcelKit;
import org.apache.commons.lang.StringUtils;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 类名：ShopAction
 * 功能：店铺管理Action
 * 作者：Tony
 * 日期：2017/8/22.
 */
public class ShopAction extends BaseAction {

    private Integer offset;
    private Integer limit;
    private String shop_mobile;
    private String city;
    private String area;
    private String shop_name;
    private Integer check_status=-3;
    private Integer customer_id;
    private String contacts;
    private String channel_type;
    private String customer_no;
    private String office_address;
    private String route_id;
    private String office_address2;
    private String route_id2;
    //搜索条件的店铺编号
    private String customer_system_no;
    private String fileUrl;
    private String filename;
    //查询是否有指定经销商时用的店铺编号
    private String customer_system_no2;
    private String area_no;

    public String getArea_no() {
        return area_no;
    }

    public void setArea_no(String area_no) {
        this.area_no = area_no;
    }

    public String getCustomer_system_no2() {
        return customer_system_no2;
    }

    public void setCustomer_system_no2(String customer_system_no2) {
        this.customer_system_no2 = customer_system_no2;
    }

    public String getFileUrl() {
        return fileUrl;
    }

    public void setFileUrl(String fileUrl) {
        this.fileUrl = fileUrl;
    }

    public String getFilename() {
        return filename;
    }

    public void setFilename(String filename) {
        this.filename = filename;
    }

    public String getCustomer_system_no() {
        return customer_system_no;
    }

    public void setCustomer_system_no(String customer_system_no) {
        this.customer_system_no = customer_system_no;
    }

    public String getOffice_address2() {
        return office_address2;
    }

    public void setOffice_address2(String office_address2) {
        this.office_address2 = office_address2;
    }

    public String getRoute_id2() {
        return route_id2;
    }

    public void setRoute_id2(String route_id2) {
        this.route_id2 = route_id2;
    }

    public String getCustomer_no() {
        return customer_no;
    }

    public void setCustomer_no(String customer_no) {
        this.customer_no = customer_no;
    }

    public String getOffice_address() {
        return office_address;
    }

    public void setOffice_address(String office_address) {
        this.office_address = office_address;
    }

    public String getRoute_id() {
        return route_id;
    }

    public void setRoute_id(String route_id) {
        this.route_id = route_id;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public void setChannel_type(String channel_type) {
        this.channel_type = channel_type;
    }

    public String getChannel_type() {
        return channel_type;
    }

    public String getContacts() {
        return contacts;
    }

    public void setContacts(String contacts) {
        this.contacts = contacts;
    }

    public Integer getCustomer_id() {
        return customer_id;
    }

    public void setCustomer_id(Integer customer_id) {
        this.customer_id = customer_id;
    }

    public Integer getCheck_status() {
        return check_status;
    }

    public void setCheck_status(Integer check_status) {
        this.check_status = check_status;
    }

    public String getShop_mobile() {
        return shop_mobile;
    }

    public void setShop_mobile(String shop_mobile) {
        this.shop_mobile = shop_mobile;
    }

    public Integer getOffset() {
        return offset;
    }

    public void setOffset(Integer offset) {
        this.offset = offset;
    }

    public Integer getLimit() {
        return limit;
    }

    public void setLimit(Integer limit) {
        this.limit = limit;
    }


    public String getArea() {
        return area;
    }

    public void setArea(String area) {
        this.area = area;
    }

    public String getShop_name() {
        return shop_name;
    }

    public void setShop_name(String shop_name) {
        this.shop_name = shop_name;
    }

    /**
     * 功能：跳转到店铺列表页面
     * 返回：SUCCESS代表跳转成功
     */
    public String toShopsList(){
        return SUCCESS;
    }

    /**
     * 功能：获取店铺列表
     */
    public void getShopList(){
        Map<String,Object> resultMap = new HashMap<String,Object>();

        resultMap.put("rows", ShopService.getInstance().getShopList(offset,limit,getShopSearchFilter()));

        resultMap.put("total",ShopService.getInstance().getShopCount(getShopSearchFilter()));

        Struts2Utils.renderJson(resultMap);
    }

    /**
     * 功能：获取搜索店铺的搜索条件
     * 返回：搜索条件
     */
    private String getShopSearchFilter(){
        StringBuilder filter = new StringBuilder("");
        if(StringUtils.isNotEmpty(shop_mobile)){
            filter.append(" and customer_mobile='").append(shop_mobile).append("'");
        }
        if(StringUtils.isNotEmpty(city)){
            filter.append(" and city='").append(city).append("'");
        }
        if(StringUtils.isNotEmpty(area)){
            String area_no = ShopService.getInstance().getAreaNoByAreaName(area);
            filter.append("  and area='").append(area_no).append("'");
        }
        if(StringUtils.isNotEmpty(channel_type)){
            filter.append(" and channel_type_no='").append(channel_type).append("'");
        }
        if(check_status>-3){
            filter.append(" and check_status='").append(check_status).append("'");
        }
        if(StringUtils.isNotEmpty(office_address2)){
            filter.append(" and office_address='").append(office_address2).append("'");
        }
        if(StringUtils.isNotEmpty(route_id2)){
            filter.append(" and route_id='").append(route_id2).append("'");
        }
        if(StringUtils.isNotEmpty(customer_system_no)){
            filter.append(" and customer_system_no='").append(customer_system_no).append("'");
        }
        System.out.println(filter.toString());
        return filter.toString();
    }

    /**
     * 功能：根据店铺编号获取店铺信息
     */
    public void getCustomerInfoById(){
        Map<String, Object> resultMap = new HashMap<String, Object>();
        Map<String, Object> customerInfoMap = ShopService.getInstance().getCustomerInfoById(customer_id,customer_system_no);
        String city_name = getAreaNameByAreaNo((String) customerInfoMap.get("city"));
        String area_name = getAreaNameByAreaNo((String) customerInfoMap.get("area"));
        String channel_type = getChannelType((String) customerInfoMap.get("channel_type_no"));
        customerInfoMap.put("city",city_name);
        customerInfoMap.put("area",area_name);
        customerInfoMap.put("channel_type_no",channel_type);
        if(customerInfoMap==null){
            resultMap.put("result","error");
        }else{
            resultMap.put("result","success");
            resultMap.put("customerInfo",customerInfoMap);
        }

        Struts2Utils.renderJson(resultMap);
    }

    /**
     * 根据地区编号获取地区名称
     * @return
     * @author tony
     * @since 2017/9/7 15:22
     */
    public String getAreaNameByAreaNo(String area_no){
        return ShopService.getInstance().getAreaNameByAreaNo(area_no);
    }

    /**
     * 根据渠道类型编号获取渠道名称
     * @param channel_type_no 渠道类型编号
     * @return
     * @author tony
     * @since 2017/9/11 11:19
     */
    public String getChannelType(String channel_type_no){
        return ShopService.getInstance().getChannelType(channel_type_no);
    }

    /**
     * 功能：更改状态
     */
    public void updateCheckStatus(){
        Map<String, Object> resultMap = new HashMap<String, Object>();
        if(ShopService.getInstance().updateCheckStatus(customer_id,check_status)){
            resultMap.put("result","success");
        }else{
            resultMap.put("result","error");
        }
        Struts2Utils.renderJson(resultMap);
    }

    /**
     * 功能：管理店铺
     */
    public void updateManagerCustomer(){
        Map<String, Object> resultMap = new HashMap<String, Object>();
        if (ShopService.getInstance().updateManagerCustomer(customer_id, customer_no, office_address, route_id)){
            resultMap.put("result","success");
        }else{
            resultMap.put("result","error");
        }
        Struts2Utils.renderJson(resultMap);
    }

    /**
     * 功能：上传店铺信息
     * 作者：tony
     */
    public void importShopInfo(){
        Map<String,Object> resultMap = new HashMap<String, Object>();
        resultMap = ShopService.getInstance().handleShopInfo(fileUrl,getUserName(),filename,getUserId());
        Struts2Utils.renderJson(resultMap);
    }

    /**
     * 导出店铺信息
     * @return
     * @author tony
     * @since 2017/9/6 14:54
     */
    public String exportShopInfo() throws Exception {
        String filters = ShopService.getInstance().getFilters(this.getRequest());
        List<ExportShop> list = ShopService.getInstance().getExportShopList(filters);
        if (list.size() != 0) {
            ExcelKit.$Export(ExportShop.class, getResponse()).toExcel(list, "店铺信息表");
            return SUCCESS;
        }else{
            return ERROR;
        }
    }
}
